// ==UserScript==
// @name         Spans Fly Everywhere!
// @namespace    http://tampermonkey.net/
// @version      2025-08-09
// @description  让所有网站的<span>标签都乱飞起来！
// @author       Your Name
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @run-at       document-idle
// @grant        GM_addStyle
// @grant        GM_openInTab
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        unsafeWindow
// @updateURL  https://gitee.com/kingstk/script/raw/master/parody.meta.js
// @downloadURL  https://gitee.com/kingstk/script/raw/master/parody.user.js
// ==/UserScript==

(function() {
    'use strict';

    const flyingSpans = new Set();
    let flyInterval = null;

    function applyFlyStyles(span) {
        if (flyingSpans.has(span)) {
            return;
        }

        span.style.position = 'fixed';
        span.style.transition = 'all 5s ease-in-out';
        span.style.zIndex = '9999';
        span.style.pointerEvents = 'none';
        span.style.whiteSpace = 'nowrap';
        span.style.willChange = 'transform, top, left';

        span.style.backgroundColor = `hsl(${Math.random() * 360}, 70%, 80%)`;
        span.style.padding = '5px 8px';
        span.style.borderRadius = '4px';
        span.style.boxShadow = '0 2px 8px rgba(0,0,0,0.2)';
        span.style.border = '1px solid rgba(0,0,0,0.1)';

        flyingSpans.add(span);
    }

    function moveSpanRandomly(span) {
        const viewportWidth = window.innerWidth;
        const viewportHeight = window.innerHeight;

        const spanRect = span.getBoundingClientRect();
        const spanWidth = spanRect.width;
        const spanHeight = spanRect.height;

        const newLeft = Math.random() * (viewportWidth - spanWidth);
        const newTop = Math.random() * (viewportHeight - spanHeight);

        span.style.left = `${newLeft}px`;
        span.style.top = `${newTop}px`;
    }

    function findAndProcessNewSpans(rootElement = document) {
        const spans = rootElement.querySelectorAll('span');
        let newSpansFound = 0;

        spans.forEach(span => {
            if (!flyingSpans.has(span)) {
                applyFlyStyles(span);
                moveSpanRandomly(span);
                newSpansFound++;
            }
        });

        if (newSpansFound > 0) {
            console.log(`[Spans Fly Everywhere] 发现并处理了 ${newSpansFound} 个新的<span>标签。`);
        }
    }

    function startSpansFlying() {
        findAndProcessNewSpans();

        if (flyingSpans.size === 0) {
            console.log("[Spans Fly Everywhere] 页面上没有找到<span>标签，恶搞函数未启动。");
            return;
        }

        console.log(`[Spans Fly Everywhere] 检测到 ${flyingSpans.size} 个<span>标签，准备开始乱飞！`);

        flyInterval = setInterval(() => {
            flyingSpans.forEach(moveSpanRandomly);
        }, 5000);

        window.spanFlyInterval = flyInterval;
        console.log("[Spans Fly Everywhere] <span>标签已开始乱飞！要停止请在控制台运行：clearInterval(window.spanFlyInterval);");

        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                if (mutation.addedNodes.length > 0) {
                    mutation.addedNodes.forEach(node => {
                        if (node.nodeType === Node.ELEMENT_NODE) {
                            if (node.tagName === 'SPAN') {
                                applyFlyStyles(node);
                                moveSpanRandomly(node);
                            }
                            findAndProcessNewSpans(node);
                        }
                    });
                }
            });
        });

        observer.observe(document.body, { childList: true, subtree: true });
        console.log("[Spans Fly Everywhere] MutationObserver 已启动，将监听动态添加的<span>标签。");
    }

    setTimeout(()=>{
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', startSpansFlying);
        } else {
            startSpansFlying();
        }
    },10000)


    window.stopSpansFlying = function() {
        if (flyInterval) {
            clearInterval(flyInterval);
            console.log("[Spans Fly Everywhere] <span>标签已停止乱飞。");
            flyInterval = null;
        }
        flyingSpans.forEach(span => {
            span.style.position = '';
            span.style.transition = '';
            span.style.zIndex = '';
            span.style.pointerEvents = '';
            span.style.whiteSpace = '';
            span.style.willChange = '';
            span.style.backgroundColor = '';
            span.style.padding = '';
            span.style.borderRadius = '';
            span.style.boxShadow = '';
            span.style.border = '';
        });
        flyingSpans.clear();
    };

})();
